<?php
/* SVN FILE: $Id: app_controller.php 7296 2008-06-27 09:09:03Z gwoo $ */
/**
 * Short description for file.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake.libs.controller
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 7296 $
 * @modifiedby		$LastChangedBy: gwoo $
 * @lastmodified	$Date: 2008-06-27 05:09:03 -0400 (Fri, 27 Jun 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.cake.libs.controller
 */
class AppController extends Controller {
	var $admin = array();
    var $allowedActions = null;
    var $components = array('Auth');
    function beforeFilter(){
        //Override default fields used by Auth component
        $this->Auth->fields = array('username'=>'username','password'=>'password');
        //Set application wide actions which do not require authentication
//        $this->Auth->allow('*');
        //Set the default redirect for users who logout
        $this->Auth->logoutRedirect = '/';
        //Set the default redirect for users who login
        $this->Auth->loginRedirect = '/users/redirectLogin';
        //Extend auth component to include authorisation via isAuthorized action
        $this->Auth->authorize = 'controller';
        //Restrict access to only users marked as active
        //$this->Auth->userScope = array('User.active = 1');
        //Pass auth component data over to view files
        $this->set('Auth',$this->Auth->user());
        //Message when not permissions
        //$this->Auth->authError = "Bạn không có quyền vào đây. <br /> Xin mời bạn đăng nhập";
        
    }
    function beforeRender(){
        //If we have an authorised user logged in then set up the admin element
        if($this->Auth->user()){
            $controllerList = Configure::listObjects('controller');
            foreach($controllerList as $controllerItem){
                if($controllerItem <> 'App'){
                    if($this->__permitted($controllerItem,'index')){
                        $this->admin[] = $controllerItem;
                    }
                }
            }
        }
        $this->set('admin',$this->admin);
    }
    function isAuthorized(){
        return $this->__permitted($this->name,$this->action);
    }
    function __permitted($thisController,$thisAction){
        //Ensure checks are all made lower case
        $thisController = low($thisController);
        $thisAction = low($thisAction);
        //Firstly users:logout is never restricted so set up a bypass
        if($thisController.':'.$thisAction == 'users:logout'){
            return true;
        }
        //Now if allowedActions has not yet benn created, create it
        if(!$this->allowedActions){
            //Import the User Model so we can build up the permission cache
            App::import('Model', 'User');
            $thisUser = new User;
            $thisGroup = new Group;
            //Now bring in the current users full record along with groups
            $thisGroups = $thisUser->find(array('User.id'=>$this->Auth->user('id')));
            $thisGroup = $thisGroups['Group'];
            $thisPermissions = $thisUser->Group->find(array('Group.id'=>$thisGroup['id']));
            $thisPermissions = $thisPermissions['Permission'];
            foreach($thisPermissions as $thisPermission){
            	$this->allowedActions[]=$thisPermission['name'];
            }
        }
        //Now check the cache to see if we have permission set
        foreach($this->allowedActions as $allowedAction){
            if($allowedAction == '*'){
                return true;//Super Admin Bypass Found
            }
            if($allowedAction == $thisController.':'.$thisAction){
                return true;//Specific permission found
            }
        }
        
        return false;
    }	
    
/**
 * undocumented function
 *
 * @param string $model
 * @return void
 * @access public
 */

  function pageForPagination($model) {

    $page = 1;

    $sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;

    $pageInUrl = isset($this->params['named']['page']);

    if ($sameModel && $pageInUrl) {

      $page = $this->params['named']['page'];

    }

    $this->passedArgs['page'] = $page;

    return $page;

  }
}
?>